package com.jiawei.note.widgets;

import android.content.Context;
import android.content.res.TypedArray;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.jiawei.note.R;
import com.jiawei.note.adapter.TypeAdapter;
import com.jiawei.note.entity.TypeEntity;

import java.util.ArrayList;
import java.util.List;

import androidx.annotation.Nullable;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;


public class TitleBarLayout extends LinearLayout {
    private Context mContext;
    //控件
    private ImageView ivLeft,ivRightOne,ivRightTwo;
    private TextView tvAll;
    private TextView tvLeft,tvRight;
    //数据
    private String strLeft,strRight;
    private int iconLeft,iconRightOne,iconRightTwo;

    //下拉框的数据源
    private List<TypeEntity> mList = new ArrayList<>();

    public void setList(List<TypeEntity> list) {
        mList.clear();
        mList.addAll(list);
        if(mTypeAdapter != null){
            mTypeAdapter.notifyDataSetChanged();
        }
    }

    public TitleBarLayout(Context context) {
        super(context);
    }

    public TitleBarLayout(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        this.mContext = context;
        //获得自定义属性
        TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.TitleBarLayout);
        strLeft = typedArray.getString(R.styleable.TitleBarLayout_strLeft);
        strRight = typedArray.getString(R.styleable.TitleBarLayout_strRight);
        iconLeft = typedArray.getResourceId(R.styleable.TitleBarLayout_iconLeft,0);
        iconRightOne = typedArray.getResourceId(R.styleable.TitleBarLayout_iconRightOne,0);
        iconRightTwo = typedArray.getResourceId(R.styleable.TitleBarLayout_iconRightTwo,0);
        typedArray.recycle();//记得回收
        //加载自定义布局
        LayoutInflater.from(context).inflate(R.layout.layout_title, this);
        initViews();


    }

    private void initViews() {
        ivLeft = (ImageView) findViewById(R.id.iv_left);
        tvLeft = (TextView) findViewById(R.id.tv_left);
        tvAll = (TextView) findViewById(R.id.tv_all);
        ivRightTwo = (ImageView) findViewById(R.id.iv_right_two);
        ivRightOne = (ImageView) findViewById(R.id.iv_right_one);
        tvRight = (TextView) findViewById(R.id.tv_right);

        if(strLeft!= null){
            tvLeft.setVisibility(VISIBLE);
            tvLeft.setText(strLeft);
        }

        if(strRight!= null){
            tvRight.setVisibility(VISIBLE);
            tvRight.setText(strRight);
        }

        if(iconLeft != 0){
            ivLeft.setVisibility(VISIBLE);
            ivLeft.setImageResource(iconLeft);
        }
        if(iconRightOne != 0){
            ivRightOne.setVisibility(VISIBLE);
            ivRightOne.setImageResource(iconRightOne);
        }
        if(iconRightTwo != 0){
            ivRightTwo.setVisibility(VISIBLE);
            ivRightTwo.setImageResource(iconRightTwo);
        }

        //全部设置点击事件
        tvAll.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                tvAll.setCompoundDrawables(null,null,mContext.getDrawable(R.drawable.ic_less),null);
                showPopupWindow(tvAll);

            }
        });
    }
    private PopupWindow mPopupWindow;
    private TypeAdapter mTypeAdapter;
    private void showPopupWindow(TextView tvAll) {
            if(mPopupWindow != null && mPopupWindow.isShowing()){
               return;
            }
            mPopupWindow = new PopupWindow(mContext);
            mPopupWindow.setWidth(700);
            mPopupWindow.setHeight(1000);
            mPopupWindow.setBackgroundDrawable(mContext.getDrawable(R.drawable.shape_type_bg));
            View view = LayoutInflater.from(getContext()).inflate(R.layout.layout_popupwindow,null);
            //列表展示
            RecyclerView  recyclerView = view.findViewById(R.id.rv);
            mTypeAdapter = new TypeAdapter(R.layout.item_type,mList);
            recyclerView.setAdapter(mTypeAdapter);
            recyclerView.setLayoutManager(new LinearLayoutManager(mContext));

            mPopupWindow.setContentView(view);
            mPopupWindow.showAsDropDown(tvAll,-310,0);
            mPopupWindow.setOutsideTouchable(true);//点击外部自动关闭
            mPopupWindow.setOnDismissListener(new PopupWindow.OnDismissListener() {
                @Override
                public void onDismiss() {
                    tvAll.setCompoundDrawables(null,null,getContext().getDrawable(R.drawable.ic_more),null);

                }
            });


    }

    public TitleBarLayout(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    public TitleBarLayout(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
    }






}
